package com.app.service.impl;

import com.app.mapper.DepartmentMapper;
import com.app.mapper.ProjectProgressMapper;
import com.app.mapper.StatusMapper;
import com.app.mapper.SystemCategoryMapper;
import com.app.model.DepartmentModel;
import com.app.model.ProjectModel;
import com.app.model.StatusModel;
import com.app.model.SystemModel;
import com.app.service.ProjectProgressService;
import com.app.vo.ProjectProgressVo;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.text.StringEscapeUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

@Service
public class ProjectProgressServiceImpl implements ProjectProgressService {
    @Autowired
    private ProjectProgressMapper projectProgressMapper;
    @Autowired
    private DepartmentMapper departmentMapper;
    @Autowired
    private StatusMapper statusMapper;
    @Autowired
    private SystemCategoryMapper systemCategoryMapper;

    //查全部
    @Override
    public ProjectProgressVo projectList(ProjectModel model) {
        List<ProjectModel> modelList = getLIstByModel(model);

        List<DepartmentModel> departmentList = getDepartmentList();
        if (departmentList.size() != 0) {
            for (int i = 0; i < departmentList.size(); i++) {
                for (int i1 = 0; i1 < modelList.size(); i1++) {
                    if (modelList.get(i1).getDepartmentId() == departmentList.get(i).getId()) {
                        modelList.get(i1).setDepartmentText(departmentList.get(i).getUnitName());
                    }
                }
            }
        }
        List<SystemModel> projectClass = getProjectClass();
        for (int i = 0; i < projectClass.size(); i++) {
            for (int i1 = 0; i1 < modelList.size(); i1++) {
                if (modelList.get(i1).getClassId() == projectClass.get(i).getId()) {
                    modelList.get(i1).setClassText(projectClass.get(i).getName());
                }
            }
        }
        List<SystemModel> projectType = getProjectType();
        for (int i = 0; i < projectType.size(); i++) {
            for (int i1 = 0; i1 < modelList.size(); i1++) {
                if (modelList.get(i1).getType() == projectType.get(i).getId()) {
                    modelList.get(i1).setTypeText(projectType.get(i).getName());
                }
            }
        }
        List<SystemModel> statusList = getStatusList();
        for (int i = 0; i < statusList.size(); i++) {
            for (int i1 = 0; i1 < modelList.size(); i1++) {
                if (modelList.get(i1).getStatus() == statusList.get(i).getId()) {
                    modelList.get(i1).setStatusText(statusList.get(i).getName());
                }
            }
        }

        for (int i = 0; i < modelList.size(); i++) {
            if (modelList.get(i).getStatus() == 0) {
                modelList.get(i).setStatusText("-");
            }
        }
        int count = getCountByModel(model);
        ProjectProgressVo vo = new ProjectProgressVo();
        vo.setList(modelList);
        vo.setCount(count);
        return vo;
    }

    //按类型查
    @Override
    public ProjectProgressVo getListByType(ProjectModel model) {
        List<ProjectModel> modelList = getList(model);
        List<DepartmentModel> departmentList = getDepartmentList();
        for (int i = 0; i < departmentList.size(); i++) {
            for (int i1 = 0; i1 < modelList.size(); i1++) {
                if (modelList.get(i1).getDepartmentId() == departmentList.get(i).getId()) {
                    modelList.get(i1).setDepartmentText(departmentList.get(i).getUnitName());
                }
            }
        }
        List<SystemModel> projectClass = getProjectClass();
        for (int i = 0; i < projectClass.size(); i++) {
            for (int i1 = 0; i1 < modelList.size(); i1++) {
                if (modelList.get(i1).getClassId() == projectClass.get(i).getId()) {
                    modelList.get(i1).setClassText(projectClass.get(i).getName());
                }
            }
        }

        List<SystemModel> projectType = getProjectType();
        for (int i = 0; i < projectType.size(); i++) {
            for (int i1 = 0; i1 < modelList.size(); i1++) {
                if (modelList.get(i1).getType() == projectType.get(i).getId()) {
                    modelList.get(i1).setTypeText(projectType.get(i).getName());
                }
            }
        }
        List<SystemModel> statusList = getStatusList();
        for (int i = 0; i < statusList.size(); i++) {
            for (int i1 = 0; i1 < modelList.size(); i1++) {
                if (modelList.get(i1).getStatus() == statusList.get(i).getId()) {
                    modelList.get(i1).setStatusText(statusList.get(i).getName());
                }
            }
        }
        for (int i = 0; i < modelList.size(); i++) {
            if (modelList.get(i).getStatus() == 0) {
                modelList.get(i).setStatusText("-");
            }
        }
        int count = getCount(model);
        ProjectProgressVo vo = new ProjectProgressVo();
        vo.setList(modelList);
        vo.setCount(count);
        return vo;
    }

    @Override
    public List<ProjectModel> getLIstByModel(ProjectModel model) {
        return projectProgressMapper.getLIstByModel(model);
    }

    @Override
    public int getCountByModel(ProjectModel model) {
        return projectProgressMapper.getCountByModel(model);
    }

    @Override
    public List<ProjectModel> getList(ProjectModel model) {
        return projectProgressMapper.getList(model);
    }

    @Override
    public int getCount(ProjectModel model) {
        return projectProgressMapper.getCount(model);
    }

    @Override
    public void deleteProjectById(int id) {
        projectProgressMapper.deleteById(id);
    }

    @Override
    public void update(ProjectModel model) {
        projectProgressMapper.updateById(model);
    }

    @Override
    public int insert(ProjectModel model) {
        return projectProgressMapper.insert(model);
    }

    @Override
    public void updateStatus(StatusModel model) {
        //如果project_id不存在 新增
        List<StatusModel> statusModel = statusMapper.selectByProjectId(model.getProjectId());
        if (statusModel == null) {
            model.setCreateTime(LocalDateTime.now());
            model.setUpdateTime(LocalDateTime.now());
            statusMapper.insert(model);
            return;
        }
        //存在,根据id更新内容
        for (int i = 0; i < statusModel.size(); i++) {
            if (statusModel.get(i).getStatus() == model.getStatus()) {
                model.setUpdateTime(LocalDateTime.now());
                int projectId = model.getProjectId();
                String remarks = model.getRemarks();
                Integer status = model.getStatus();
                statusMapper.updateByProjectId(projectId, remarks, status);
                ProjectModel model1 = projectProgressMapper.selectById(projectId);
                model1.setStatus(model.getStatus());
                projectProgressMapper.updateById(model1);
                return;
            }
        }

        model.setCreateTime(LocalDateTime.now());
        model.setUpdateTime(LocalDateTime.now());
        statusMapper.insert(model);
        int projectId = model.getProjectId();
        ProjectModel model1 = projectProgressMapper.selectById(projectId);
        model1.setStatus(model.getStatus());
        projectProgressMapper.updateById(model1);
    }

    @Override
    public ProjectProgressVo List(ProjectModel model) {
        List<ProjectModel> listByKey = projectProgressMapper.getListByKey(model);
        int countByKey = projectProgressMapper.getCountByKey(model);
        ProjectProgressVo vo = new ProjectProgressVo();
        vo.setList(listByKey);
        vo.setCount(countByKey);
        return vo;
    }

    @Override
    public ProjectProgressVo getAllByTime(ProjectModel model) {
        List<ProjectModel> listByKey = projectProgressMapper.getListByTime(model);
        List<DepartmentModel> departmentList = getDepartmentList();
        for (int i = 0; i < departmentList.size(); i++) {
            for (int i1 = 0; i1 < listByKey.size(); i1++) {
                if (listByKey.get(i1).getDepartmentId() == departmentList.get(i).getId()) {
                    listByKey.get(i1).setDepartmentText(departmentList.get(i).getUnitName());
                }
            }
        }
        List<SystemModel> projectClass = getProjectClass();
        for (int i = 0; i < projectClass.size(); i++) {
            for (int i1 = 0; i1 < listByKey.size(); i1++) {
                if (listByKey.get(i1).getClassId() == projectClass.get(i).getId()) {
                    listByKey.get(i1).setClassText(projectClass.get(i).getName());
                }
            }
        }

        List<SystemModel> projectType = getProjectType();
        for (int i = 0; i < projectType.size(); i++) {
            for (int i1 = 0; i1 < listByKey.size(); i1++) {
                if (listByKey.get(i1).getType() == projectType.get(i).getId()) {
                    listByKey.get(i1).setTypeText(projectType.get(i).getName());
                }
            }
        }
        List<SystemModel> statusList = getStatusList();
        for (int i = 0; i < statusList.size(); i++) {
            for (int i1 = 0; i1 < listByKey.size(); i1++) {
                if (listByKey.get(i1).getStatus() == statusList.get(i).getId()) {
                    listByKey.get(i1).setStatusText(statusList.get(i).getName());
                }
            }
        }
        for (int i = 0; i < listByKey.size(); i++) {
            if (listByKey.get(i).getStatus() == 0) {
                listByKey.get(i).setStatusText("-");
            }
        }
        int countByKey = projectProgressMapper.getCountByTime(model);
        ProjectProgressVo vo = new ProjectProgressVo();
        vo.setList(listByKey);
        vo.setCount(countByKey);
        return vo;
    }


    @Override
    public List<DepartmentModel> getDepartmentList() {
        return departmentMapper.get();
    }

    @Override
    public DepartmentModel getDepartment(int departmentId) {
        return departmentMapper.selectById(departmentId);
    }

    @Override
    public List<StatusModel> getStatus(Integer projectId) {
        return projectProgressMapper.getStatus(projectId);
    }

    @Override
    public List<SystemModel> getProjectClass() {
        return projectProgressMapper.getProjectClass();
    }

    @Override
    public List<SystemModel> getProjectType() {
        return projectProgressMapper.getProjectType();
    }

    @Override
    public List<SystemModel> getStatusList() {
        List<SystemModel> statusList = projectProgressMapper.getStatusList();
        return statusList;
    }

    @Override
    public ProjectProgressVo getByTimeAndName(ProjectModel model) {

        List<ProjectModel> listByKey = projectProgressMapper.getByTimeAndName(model);
        List<DepartmentModel> departmentList = getDepartmentList();
        for (int i = 0; i < departmentList.size(); i++) {
            for (int i1 = 0; i1 < listByKey.size(); i1++) {
                if (listByKey.get(i1).getDepartmentId() == departmentList.get(i).getId()) {
                    listByKey.get(i1).setDepartmentText(departmentList.get(i).getUnitName());
                }
            }
        }
        List<SystemModel> projectClass = getProjectClass();
        for (int i = 0; i < projectClass.size(); i++) {
            for (int i1 = 0; i1 < listByKey.size(); i1++) {
                if (listByKey.get(i1).getClassId() == projectClass.get(i).getId()) {
                    listByKey.get(i1).setClassText(projectClass.get(i).getName());
                }
            }
        }

        List<SystemModel> projectType = getProjectType();
        for (int i = 0; i < projectType.size(); i++) {
            for (int i1 = 0; i1 < listByKey.size(); i1++) {
                if (listByKey.get(i1).getType() == projectType.get(i).getId()) {
                    listByKey.get(i1).setTypeText(projectType.get(i).getName());
                }
            }
        }
        List<SystemModel> statusList = getStatusList();
        for (int i = 0; i < statusList.size(); i++) {
            for (int i1 = 0; i1 < listByKey.size(); i1++) {
                if (listByKey.get(i1).getStatus() == statusList.get(i).getId()) {
                    listByKey.get(i1).setStatusText(statusList.get(i).getName());
                }
            }
        }
        for (int i = 0; i < listByKey.size(); i++) {
            if (listByKey.get(i).getStatus() == 0) {
                listByKey.get(i).setStatusText("-");
            }
        }
        int countByKey = projectProgressMapper.getCountByTimeAndName(model);
        ProjectProgressVo vo = new ProjectProgressVo();
        vo.setList(listByKey);
        vo.setCount(countByKey);
        return vo;
    }


    @Override
    public List<ProjectModel> getProjectByStatus(Integer status) {
        if (status == 1) {
            status = 63;
        } else if (status == 2) {
            status = 64;
        } else if (status == 3) {
            status = 65;
        }
        Calendar calendar = Calendar.getInstance();
        int year = calendar.get(Calendar.YEAR);
        List<ProjectModel> projectModelList = projectProgressMapper.getProjectByStatus(status, String.valueOf(year));
        List<StatusModel> statusModelList = getStatusModelAll();
        List<SystemModel> systemModelList = getSystemAll();
        for (int i = 0; i < projectModelList.size(); i++) {
            for (int j = 0; j < statusModelList.size(); j++) {
                if (projectModelList.get(i).getId() == statusModelList.get(j).getProjectId()) {
                    projectModelList.get(i).setRemakes(statusModelList.get(j).getRemarks());
                } else {
                    projectModelList.get(i).setRemakes("");
                }
            }
            for (int j = 0; j < systemModelList.size(); j++) {
                if (projectModelList.get(i).getClassId() == systemModelList.get(j).getId()) {
                    projectModelList.get(i).setTypeText(systemModelList.get(j).getName());
                }
            }
        }
        return projectModelList;
    }

    @Override
    public List<StatusModel> getStatusModelAll() {
        return statusMapper.selectList(null);
    }

    @Override
    public List<SystemModel> getSystemAll() {
        return systemCategoryMapper.selectList(null);
    }

    @Override
    public Integer getModelCount(Integer status) {
        QueryWrapper<ProjectModel> queryWrapper = new QueryWrapper<>();
        if (status > 0) {
            queryWrapper.select().ne("status", status);
        }
        return projectProgressMapper.selectCount(queryWrapper);
    }

    @Override
    public Integer getModelCountByStatusAndClass(Integer status, Integer classId) {
        QueryWrapper<ProjectModel> queryWrapper = new QueryWrapper<>();
        if (status > 0) {
            queryWrapper.select().eq("status", status);
        }
        if (classId > 0) {
            queryWrapper.select().eq("class_id", classId);
        }
        return projectProgressMapper.selectCount(queryWrapper);
    }

    @Override
    public void updateProject() {
        List<StatusModel> statusModels = statusMapper.selectAll();
        int i = projectProgressMapper.updateProject(statusModels);
    }

    @Override
    public ProjectModel selectCount() {
        Calendar calendar = Calendar.getInstance();
        String year = String.valueOf(calendar.get(Calendar.YEAR));
        int red = projectProgressMapper.selectRed(year);
        int blue = projectProgressMapper.selectBlue(year);
        int yellow = projectProgressMapper.selectYellow(year);
        ProjectModel model = new ProjectModel();
        model.setRedCount(red);
        model.setBlueCount(blue);
        model.setYellowCount(yellow);
        return model;
    }

    @Override
    public ProjectModel selectCountByYear(String year) {
        int red = projectProgressMapper.selectRed(year);
        int blue = projectProgressMapper.selectBlue(year);
        int yellow = projectProgressMapper.selectYellow(year);
        ProjectModel model = new ProjectModel();
        model.setRedCount(red);
        model.setBlueCount(blue);
        model.setYellowCount(yellow);
        return model;
    }

    @Override
    public ProjectModel getProjectCount() {
        ProjectModel model = new ProjectModel();
        Calendar calendar = Calendar.getInstance();
        SimpleDateFormat formats = new SimpleDateFormat("yyyy");
        //获取当前年
        int thisYear = calendar.get(Calendar.YEAR);
        //获取去年年份
        calendar.add(Calendar.YEAR, -1);
        Date time = calendar.getTime();
        String lastYear = formats.format(time);
        model.setThisYearCount(projectProgressMapper.selectByYear(String.valueOf(thisYear)));
        model.setLastYearCount(projectProgressMapper.selectByYear(lastYear));
        return model;
    }

    @Override
    public Integer selectCountByType(int id, String year) {
        return projectProgressMapper.selectCountByType(id, year);
    }
}
